﻿using EYangFramework.Business.SystemManager.Interfaces;
using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.Entrys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using EYangFrame.DataAccess.Global;

namespace EYangFramework.SystemManager
{
    public partial class frm_RoleManager : EYangFramework.CommonForm.frm_BasicForm
    {
        List<Role> roles;
        bool isAdd;
        public frm_RoleManager()
        {
            InitializeComponent();
        }

        private void frm_RoleManager_Load(object sender, EventArgs e)
        {
            this.Text = "角色管理";
            InitList();
            SetTabVisible(tp_Detail, false);
        }
        private void InitList()
        {
            tv_List.Nodes.Clear();
            using (IBLLRole bLLRole = NinjectProvider.NinjectCore.GetService<IBLLRole>())
            {
                TreeNode treeMain = new TreeNode();
                treeMain.Text = "角色清单";
                tv_List.Nodes.Add(treeMain);

                roles = bLLRole.GetAllList();
                ShowRoleList(treeMain, roles.Where(t => t.PARENT == null).OrderBy(t=>t.ROLENAME).ToList());
            }
            tv_List.ExpandAll();
        }
        private void ShowRoleList(TreeNode parent, List<Role> currentRoles)
        {
            foreach (var item in currentRoles)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Text = item.ROLENAME;
                treeNode.Tag = item.ID.ToString();
                parent.Nodes.Add(treeNode);

                List<Role> childRoles = roles.Where(t => t.PARENT?.ID == item.ID).OrderBy(t => t.ROLENAME).ToList();
                if (childRoles.Count > 0)
                {
                    ShowRoleList(treeNode, childRoles);
                }
            }
        }
        private void menu_View_Click(object sender, EventArgs e)
        {
            var currentNode = tv_List.SelectedNode;
            if (tv_List.SelectedNode == null)
            {
                MessageBox.Show("请选择需要查看的角色!");
                return;
            }
            if (tv_List.SelectedNode.Tag == null)
            {
                MessageBox.Show("你没有权限对根角色进行操作,请联系管理员!");
                return;
            }
            group_Role.Text = $"你正在查看{currentNode.Text}!";
            SetTabVisible(tp_Detail, true);
            txtRoleName.ReadOnly = true;
            txtRoleName.Text = currentNode.Text;
            btnSave.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出当前状态吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                SetTabVisible(tp_Detail, false);
            }
        }

        private void menu_Delete_Click(object sender, EventArgs e)
        {
            var currentNode = tv_List.SelectedNode;
            if (tv_List.SelectedNode == null)
            {
                MessageBox.Show("请选择需要删除的角色!");
                return;
            }
            if (tv_List.SelectedNode.Tag == null)
            {
                MessageBox.Show("你没有权限对根角色进行操作,请联系管理员!");
                return;
            }
            if (MessageBox.Show("你确定要删除当前角色吗?删除当前角色,其所有子角色也会同步删除!", "警告!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                using (IBLLRole bLLRole = NinjectProvider.NinjectCore.GetService<IBLLRole>())
                {
                    bLLRole.DeleteRole(Guid.Parse(currentNode.Tag.ToString()));
                }
                InitList();
            }
        }

        private void menu_Add_Click(object sender, EventArgs e)
        {
            var currentNode = tv_List.SelectedNode;
            if (tv_List.SelectedNode == null)
            {
                MessageBox.Show("请选择需要添加子角色的!");
                return;
            }
            SetTabVisible(tp_Detail, true);
            txtRoleName.ReadOnly = false;
            txtRoleName.Text = "";
            group_Role.Tag = currentNode.Tag;
            group_Role.Text = $"你正在为{currentNode.Text}增加子角色!";
            btnSave.Visible = true;
            isAdd = true;

        }
        private void menu_Modify_Click(object sender, EventArgs e)
        {
            var currentNode = tv_List.SelectedNode;
            if (tv_List.SelectedNode == null)
            {
                MessageBox.Show("请选择需要修改的角色!");
                return;
            }
            if (tv_List.SelectedNode.Tag == null)
            {
                MessageBox.Show("你没有权限对根角色进行操作,请联系管理员!");
                return;
            }
            isAdd = false;
            group_Role.Text = $"你正在编辑{currentNode.Text}";
            SetTabVisible(tp_Detail, true);
            group_Role.Tag = currentNode.Tag;
            txtRoleName.ReadOnly = false;
            txtRoleName.Text = currentNode.Text;
            btnSave.Visible = true;
        }

        private void tv_List_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            menu_View_Click(null, null);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoleName.Text))
            {
                MessageBox.Show("角色名不能为空!");
                return;
            }
            bool success;
            using (IBLLRole bLLRole = NinjectProvider.NinjectCore.GetService<IBLLRole>())
            {
                Role role;
                if (isAdd)
                {
                    bool isExist = bLLRole.GetAllList().Any(t => t.ROLENAME == txtRoleName.Text);
                    if (isExist)
                    {
                        MessageBox.Show("当前角色已存在,请更改角色名称!");
                        return;
                    }
                    role = new Role();
                    role.ROLENAME = txtRoleName.Text;
                    role.PARENT = group_Role.Tag == null ? null : bLLRole.GetBusinessByID(Guid.Parse(group_Role.Tag.ToString()));
                    role.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
                    success = bLLRole.InsertRole(role);
                }
                else
                {
                    role = bLLRole.GetBusinessByID(Guid.Parse(group_Role.Tag.ToString()));
                    role.ROLENAME = txtRoleName.Text;
                    success = bLLRole.UpdateRole(role);
                }
                if (success)
                {
                    MessageBox.Show("保存成功!");
                    InitList();
                    SetTabVisible(tp_Detail, false);
                }
            }
        }
        public override MenuStrip GetMenuMain()
        {
            return this.menuStrip1;
        }
    }
}
